package com.gitee.feizns.v8.rander.table;

import com.gitee.feizns.StringUtils;
import lombok.Data;

/**
 * @author feizns
 * @since 2019/8/13
 */
@Data
public class Field {

    /**
     * 注释
     */
    private String comment;

    /**
     * 字段名称
     */
    private String name;

    /**
     * 驼峰
     */
    private String javaName;

    /**
     * 主键类型
     */
    private String pkType;

    /**
     *
     */
    private String type;

    /**
     *
     */
    private String javaType;

    /**
     * jdbc类型
     */
    private String jdbcType;

    /**
     * jdbc数据类型长度
     */
    private Integer jdbcTypeLen;

    /**
     * 默认值
     */
    private Object defaultVal;

    /**
     * 是否允许为空
     */
    private boolean nullable = true;

    /**
     * 是否唯一
     */
    private boolean unique;

    public String getJavaType() {
        return Type.tryGet(type).map(Type::getJavaType).orElse(javaType);
    }

    public String getJdbcType() {
        return Type.tryGet(type).map(Type::getJdbcType).orElse(jdbcType);
    }

    public Integer getJdbcTypeLen() {
        return Type.tryGet(type).map(Type::getJdbcLength).orElse(jdbcTypeLen);
    }

    public String getPkType() {
        return pkType;
    }

    public boolean isPk() {
        return pkType != null;
    }

    public String getJavaName() {
        return javaName == null ? StringUtils.underlineToHump(name) : javaName;
    }

    public boolean isNan() {
        return !("INT".equals(getType()) || "BIGINT".equals(getType()));
    }

    public boolean isStr() {
        return "String".equals(getJavaType());
    }

    public boolean isVarChar() {
        return "VARCHAR".equals(getJdbcType());
    }

    public boolean hasDefault() {
        return defaultVal != null;
    }

}
